package com.ugoodtech.mdcc.core.utils;


import com.ugoodtech.mdcc.core.Constants;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class TomcatUploadUtil {

    public static Map<String,String> upload(String baseUrl, byte[] data, String fileType) throws Exception {
        String burl=PathUtil.formatPathWith(baseUrl);
        Map<String,String> dataMap=new HashMap<>();
        String path=System.getProperty("user.dir");
        String basePath=getWebAppPath();
        if(!StringUtils.isEmpty(basePath)){
            File filePath=new File(basePath+File.separator+burl);
            if(!filePath.exists()){
                if(!filePath.mkdirs()){
                    throw new Exception("没有创建目录的权限");
                }
            }
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat(Constants.SHORT_DATE_FORMAT);
            String fileName=simpleDateFormat.format(new Date())+BaseUtil.getRandomString(8);
            String fileStr=basePath+File.separator+burl+File.separator+fileName+"."+fileType;
            File file=new File(fileStr);
            FileUtils.writeByteArrayToFile(file,data);
            dataMap.put("path",burl+"/"+fileName+"."+fileType);
            dataMap.put("filePath",fileStr);
        }
        System.out.println("当前TomcatUploadUtil所在路径："+path);
        return dataMap;
    }


    public static Map<String,String> addOrUpdateFile(String baseUrl,String fileName, byte[] data, String fileType) throws Exception {
        String burl=PathUtil.formatPathWith(baseUrl);
        Map<String,String> dataMap=new HashMap<>();
        String path=System.getProperty("user.dir");
        String basePath=getWebAppPath();
        if(!StringUtils.isEmpty(basePath)){
            File filePath=new File(basePath+File.separator+burl);
            if(!filePath.exists()){
                if(!filePath.mkdirs()){
                    throw new Exception("没有创建目录的权限");
                }
            }
            String fileStr=basePath+File.separator+burl+File.separator+fileName+"."+fileType;
            File file=new File(fileStr);
            FileWriter writer=new FileWriter(file,true);
            writer.write(new String(data)+ "\r\n");
            writer.close();
            dataMap.put("path",burl+"/"+fileName+"."+fileType);
            dataMap.put("filePath",fileStr);
        }
        System.out.println("当前TomcatUploadUtil所在路径："+path);
        return dataMap;
    }

    public static String getWebAppPath() throws Exception {
        String path=System.getProperty("user.dir");
        String basePath=path+File.separator+"webapps"+File.separator;
        String basePath2=path+File.separator+"src"+File.separator+"main"+File.separator+"webapp"+File.separator;
        File tomcatPath=new File(basePath);
        File tomcatPath2=new File(basePath2);
        if(tomcatPath.exists()){
            return basePath;
        }else if(tomcatPath2.exists()){
            return basePath2;
        }else{
            throw new Exception("没有找到已知的tomcat路径:"+basePath+basePath2);
        }

    }



}
